import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class KlantToevoegenGUI extends JFrame implements ActionListener
{	private JButton b1,b2;
	private JTextField tf1,tf3, tf4;
	private JPasswordField pf2;
	private JLabel l1,l2,l3,l4,l0;
	private JPanel p1,p2;
	private Bank bank;
	private int hoogte=200, breedte=350;
	
	public KlantToevoegenGUI(Bank b)
	{	//the whole container
		bank = b;
		Container c = this.getContentPane();
		c.setLayout(new BorderLayout());
		
		//panels
		p1 = new JPanel();
		p1.setLayout(new GridLayout(4,2));
		c.add(p1,BorderLayout.CENTER);
		p2 = new JPanel();
		p2.setLayout(new FlowLayout());
		c.add(p2,BorderLayout.SOUTH);
		
		//add Components
		b1 = new JButton("Voeg klant toe");
		p2.add(b1);
		b2 = new JButton("Cancel");
		p2.add(b2);
		l1 = new JLabel("Naam:");
		p1.add(l1);
		tf1 = new JTextField("",5);
		p1.add(tf1);
		l2 = new JLabel("Wachtwoord:");
		p1.add(l2);
		pf2 = new JPasswordField("",5);
		p1.add(pf2);
		l3 = new JLabel("Adres:");
		p1.add(l3);
		tf3 = new JTextField("",5);
		p1.add(tf3);
		l4 = new JLabel("Woonplaats:");
		p1.add(l4);
		tf4 = new JTextField("",5);
		p1.add(tf4);
		l0 = new JLabel(" ");
		l0.setForeground(Color.RED);
		c.add(l0,BorderLayout.NORTH);
		
		//actionListeners
		b1.addActionListener(this);
		b2.addActionListener(this);
		
		//visibility and such
		this.setSize(new Dimension(breedte, hoogte));
		setTitle("Klant toevoegen");
		this.setVisible(true);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent e)
	{	if (e.getSource() == b1)
		{	String n, a, p, w;
			n = tf1.getText();
			a = tf3.getText();
			p = tf4.getText();
			w = new String(pf2.getPassword());
			if(!n.equals("") && !a.equals("") && !p.equals("") && !w.equals(""))
			{	Klant k = new Klant(n, a, p, w);
				if(!bank.medewerkers().containsKey(n))
				{	if(!bank.klanten().containsKey(n))
					{	bank.voegKlantToe(k);
						l0.setText("Klant " + n + " is toegevoegd.");
					}
					else
						l0.setText("Er bestaat al een klant met de naam " + n + ".");
				}
				else
					l0.setText("Er bestaat al een medewerker met de naam " + n + ".");
			}
			else
				l0.setText("Vul alle gegevens in.");					
		}
		else if (e.getSource() == b2)
			this.dispose();
	}
}